package com.example.demo.bean.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.example.demo.bean.Item;
import com.example.demo.bean.User;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author zxw
 * @since 2021-08-25
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "t_test", autoResultMap = true)
public class TTest implements Serializable {
    public TTest() {
    }

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    private String title;

    private String content;

    private String category;

    private Integer fid;

    @TableField(typeHandler = JacksonTypeHandler.class)
    private List<Item> itemList;

    @TableField(typeHandler = JacksonTypeHandler.class)
    private List<User> userList;

    @JsonIgnore
    public String getItemListJsonStr() {
        Gson gson = new Gson();
        String s = gson.toJson(this.itemList);
        TypeToken<ArrayList<Item>> typeToken = new TypeToken<ArrayList<Item>>() {
        };
        List<Item> items = gson.fromJson(s, typeToken.getType());
        if (items != null && !items.isEmpty()) {
            List<Integer> collect = items.stream().map(x -> Integer.parseInt(x.getId())).collect(Collectors.toList());
            return gson.toJson(collect);
        }
        return "";
    }
    @JsonIgnore
    public String getUserListJsonStr() {
        Gson gson = new Gson();
        String s = gson.toJson(this.userList);
        TypeToken<ArrayList<User>> typeToken = new TypeToken<ArrayList<User>>() {
        };
        List<User> items = gson.fromJson(s, typeToken.getType());
        if (items != null && !items.isEmpty()) {
            List<Integer> collect = items.stream().map(User::getId).collect(Collectors.toList());
            return gson.toJson(collect);
        }
        return "";
    }
}
